package com.xzz.finger;

import java.util.Stack;

/**
 * @author: hhz
 * @create: 2022-02-24 13:40
 * 剑指 Offer 31. 栈的压入、弹出序列
 **/
public class ValidateStackSequences {
    public static void main(String[] args) {
        System.out.println(validateStackSequences(new int[]{1,2,3,4,5},new int[]{4,5,3,2,1}));

        System.out.println(validateStackSequences(new int[]{1,2,3,4,5},new int[]{4,3,5,1,2}));

    }

    public static boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int pop = 0;
        for (int num : pushed) {
            stack.push(num);
            while (!stack.isEmpty() && stack.peek() == popped[pop]){
                stack.pop();
                pop++;
            }
        }
        return stack.isEmpty();
    }
}
